#1 Copy the Guinea Pig graph from plotly-1 lecture Add buttons to switch between Blue and Yellow Colors
library(plyr)
## Warning: package 'plyr' was built under R version 4.0.5
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## Warning: package 'readr' was built under R version 4.0.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::arrange() masks plyr::arrange()
## ✖ purrr::compact() masks plyr::compact()
## ✖ dplyr::count() masks plyr::count()
## ✖ dplyr::failwith() masks plyr::failwith()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::id() masks plyr::id()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::mutate() masks plyr::mutate()
## ✖ dplyr::rename() masks plyr::rename()
## ✖ dplyr::summarise() masks plyr::summarise()
## ✖ dplyr::summarize() masks plyr::summarize()
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following objects are masked from 'package:plyr':
##
## arrange, mutate, rename, summarise
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
#create date
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))
# create figure
fig <- plot_ly(tg, x = ~dose, y = ~length, type = 'scatter', mode = 'lines', linetype = ~supp, color = I('blue'))
fig <- fig %>% layout(title = 'The Effect of Vitamin C on Tooth Growth in Guinea Pigs by Supplement Type',
xaxis = list(title = 'Dose in milligrams/day'),
yaxis = list (title = 'Tooth length'),
updatemenus = list(list( type ="buttons", y = 0.7,
buttons = list(
list(method = "restyle",
args = list("line.color", "blue"),
label = "Blue"),
list(method = "restyle",
args = list("line.color", "yellow"),
label = "Yellow")))))
fig
#3 Graph from FiveThirtyEight: https://projects.fivethirtyeight.com/2022-election-forecast/house/?cid=rrpromo
Viewing the second graph down on the webpage, “Forecasting each House seat”, you’re able to see a lot of information all in one graphic. You can hover over any seat to see information about that specific race. Personally, one improvement I would make would be to change the visualization so that (at least on desktop version) it takes up the whole page, and use that extra space to help shape each states shape so that it looks more like it’s actually shape, so that it is a bit easier to read and distinguish them.